package com.id.supaclean.utils


import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent

abstract class SpecialKeysReceiver : BroadcastReceiver() {
    companion object {
        private const val SYSTEM_DIALOG_REASON_KEY = "reason"
        private const val SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps"
        private const val SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"
    }

    enum class Key {
        HOME, RECENT_APPS, SCREEN_OFF
    }

    override fun onReceive(context: Context?, intent: Intent?) {
        val action = intent?.action ?: return
        if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS == action) {
            val reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY)
            if (reason.isNullOrEmpty()) return
            if (reason == SYSTEM_DIALOG_REASON_HOME_KEY) {
                onReceive(Key.HOME)
            }
            if (reason == SYSTEM_DIALOG_REASON_RECENT_APPS) {
                onReceive(Key.RECENT_APPS)
            }
        } else if (Intent.ACTION_SCREEN_OFF == action) {
            onReceive(Key.SCREEN_OFF)
        }
    }

    protected abstract fun onReceive(key: Key)
}